package com.itydl.loveview.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.itydl.loveview.R;

import java.util.Random;

/**
 * 作者：${Howard} on 2018/2/12 02:39
 *
 * @version 1.0
 *          说明：直播点赞效果View
 */
public class LoveView extends RelativeLayout {
    //添加  ♥  的数据源
    private int[] images;
    private Random mRandom;
    private int mWidth;
    private int mHeight;
    private int mDrawableWidth;
    private int mDrawableHeight;

    private Interpolator[] mInterpolator;

    public LoveView(Context context) {
        this(context,null);
    }

    public LoveView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        images = new int[]{R.drawable.pl_blue,R.drawable.pl_red,R.drawable.pl_yellow};
        mRandom = new Random();

        // 获取图片的宽高
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.pl_blue);
        mDrawableWidth = drawable.getIntrinsicWidth();
        mDrawableHeight = drawable.getIntrinsicHeight();

        mInterpolator = new Interpolator[]{new AccelerateDecelerateInterpolator(),new AccelerateInterpolator(),
                new DecelerateInterpolator(),new LinearInterpolator()};
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取自己的宽高
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    /**
     * 点击添加心形  ♥  View
     */
    public void addLove() {
        final ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(images[mRandom.nextInt(images.length)]);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(CENTER_HORIZONTAL);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM);
        imageView.setLayoutParams(layoutParams);
        addView(imageView);

        AnimatorSet animatorSet = doAddAnimation(imageView);
        animatorSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                //所有动画执行完毕,移除该ImageView放置占用内存
                removeView(imageView);
            }
        });

        animatorSet.start();

    }

    /**
     *  ♥ 添加时的动画
     *  1、缩放动画  0-->1
     *  2、渐变动画  0-->1
     * @param imageView
     */
    private AnimatorSet doAddAnimation(ImageView imageView) {
        //总集合（包含贝塞尔动画效果）
        AnimatorSet animatorSet = new AnimatorSet();

        /**---------  添加   ♥  View动画集合 ---------**/
        AnimatorSet innerAnimatorSet = new AnimatorSet();
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView,"Alpha",0.3f,1.0f);
        ObjectAnimator scalXAnimator = ObjectAnimator.ofFloat(imageView,"ScaleX",0.3f,1.0f);
        ObjectAnimator scalYAnimator = ObjectAnimator.ofFloat(imageView,"ScaleY",0.3f,1.0f);
        innerAnimatorSet.playTogether(alphaAnimator,scalXAnimator,scalYAnimator);
        innerAnimatorSet.setDuration(300);


        /**---------  ♥轨迹以及渐变动画  ---------**/
        ValueAnimator bezerAnimation = doBezerAnimation(imageView);


        animatorSet.playSequentially(innerAnimatorSet,bezerAnimation);
        //总时长为两个时间的和
//        animatorSet.setDuration(5000);

        return animatorSet;
    }

    /**
     * 专门做贝塞尔曲线运动轨迹逻辑
     * @param imageView
     * @return
     */
    private ValueAnimator doBezerAnimation(final ImageView imageView) {

        //注意计算p0点的x
        PointF pointP0 = new PointF(mWidth/2 - mDrawableWidth/2,mHeight - mDrawableHeight);
        PointF pointP3 = new PointF((mRandom.nextInt(mWidth)-mDrawableWidth)<0?0:mRandom.nextInt(mWidth)-mDrawableWidth,0);

        //x从0--->mWidth，y从mHeight/2-->mHeight
        PointF mPointP1 = new PointF((mRandom.nextInt(mWidth)-
                mDrawableWidth)<0?0:mRandom.nextInt(mWidth)-mDrawableWidth,mHeight/2 + mRandom.nextInt(mWidth));
        //x从0--->mWidth，y从0-->mHeight/2
        PointF mPointP2 = new PointF((mRandom.nextInt(mWidth)-
                mDrawableWidth)<0?0:mRandom.nextInt(mWidth)-mDrawableWidth,mRandom.nextInt(mHeight/2));


        ValueAnimator bezierAnimator = ObjectAnimator.
                ofObject(new BezerEvaluator(mPointP1,mPointP2),pointP0,pointP3);

        bezierAnimator.setInterpolator(mInterpolator[mRandom.nextInt(mInterpolator.length)]);
        bezierAnimator.setDuration(3000);

        bezierAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                imageView.setAlpha(1-animation.getAnimatedFraction()+0.2f);//1--->0.2f
            }
        });

        return bezierAnimator;
    }
}
